package com.guigu.serviceorder.components;

import com.guigu.serviceorder.entity.TbStatistical;
import com.guigu.serviceorder.service.CategoryReportService;
import com.guigu.serviceorder.service.ITbOrderConfigService;
import com.guigu.serviceorder.service.ITbOrderService;
import com.guigu.serviceorder.service.ITbStatisticalService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.util.Date;

/**
 * 与订单相关的定时任务的定时器
 */
@Component
public class OrderTask {

    @Autowired
    private CategoryReportService categoryReportService;

    @Autowired
    private ITbStatisticalService iTbStatisticalService;

    @Autowired
    private ITbOrderService iTbOrderService;


    @Autowired
    private ITbOrderConfigService iTbOrderConfigService;

    //秒杀订单超过多少分钟自动关闭

    //正常订单超过多少分钟自动关闭

    //发货超时超过多少天自动关闭

    //订单完成超过多少天自动关闭

    //订单完成超过多少天自动关闭
//    @Scheduled(cron = "0 0/2 * * * ?")
//    public void orderTimeoutLogic(){
//        System.out.println("每隔两分钟查询超时订单" + new Date());
//        iTbOrderConfigService.orderTimeOut();
//        iTbOrderConfigService.seckillOrderTimeOut();
//        iTbOrderConfigService.serviceOrderTimeOut();
//        iTbOrderConfigService.orderTimeOut();
//        iTbOrderConfigService.commentOrderTimeOut();
//    }


    /**
     * 每天凌晨1点时执行该定时任务
     */
    @Scheduled(cron = "0 0 1 * * ?")
    public void createCategoryReportData() {
        System.out.println("生成类目统计数据");
        categoryReportService.createData();

        TbStatistical tbStatistical=new TbStatistical();
        TbStatistical selectnumber = iTbStatisticalService.selectnumber();
        TbStatistical selectamount = iTbStatisticalService.selectamount();
        TbStatistical selectonum = iTbStatisticalService.selectonum();
        TbStatistical selectorders = iTbStatisticalService.selectorders();
        TbStatistical selectovalid = iTbStatisticalService.selectovalid();
        TbStatistical selectpayment = iTbStatisticalService.selectpayment();
        TbStatistical selectquantity = iTbStatisticalService.selectquantity();

        if (selectnumber!=null){
            tbStatistical.setNumber(selectnumber.getNumber());
        }
        if (selectorders!=null){
            tbStatistical.setOrders(selectorders.getOrders());
            tbStatistical.setOmoney(selectorders.getOmoney());
        }
        if (selectonum!=null){
            tbStatistical.setOnum(selectonum.getOnum());
        }
        if (selectovalid!=null){
            tbStatistical.setOvalid(selectovalid.getOvalid());
        }
        if (selectamount!=null){
            tbStatistical.setAmount(selectamount.getAmount());
        }
        if (selectpayment!=null){
            tbStatistical.setPayment(selectpayment.getPayment());
        }
        if (selectquantity!=null){
            tbStatistical.setQuantity(selectquantity.getQuantity());
            tbStatistical.setPamount(selectquantity.getPamount());
            tbStatistical.setPrice(selectquantity.getPrice());
        }
        boolean save = iTbStatisticalService.save(tbStatistical);
    }

}
